#include "page_video.h"
#include "ui_page_video.h"
#include "udpthread.h"
#include <qprocess.h>
#include <QDebug>
#include <QTimer>
#include <QImage>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/opencv.hpp>


Page_video::Page_video(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Page_video)
{
    ui->setupUi(this);


    m_udpSocket = new QUdpSocket(this);
//    m_udpSocket->bind(QHostAddress("127.0.0.1"), 8000);
    m_udpSocket->bind(QHostAddress("192.168.1.110"), 8000);  //开发板ip


//    QByteArray message = "PEOPLE";
//    QByteArray message2 = "FIRE";
//    m_udpSocket->writeDatagram(message2, QHostAddress("127.0.0.1"),9000);
//    qDebug() << "信息已发送";

//    connect(m_udpSocket, &QUdpSocket::readyRead, this, &Page_video::readPendingDatagrams);
    // 调整代码
//    QSize widgetSize = ui->widget_left->size();
//    ui->label_left->setFixedSize(widgetSize); // 设置标签尺寸与小部件相同

//    QSize widgetSize2 = ui->widget_right->size();
//    ui->label_right->setFixedSize(widgetSize2);

    m_udpThread = new UdpThread(m_udpSocket);
    m_udpThread->start();
    connect(m_udpThread, &UdpThread::imgReturned, this, &Page_video::showImg);


    // 安装事件过滤器
    ClickEventFilter* clickFilter_left = new ClickEventFilter(this);
    ClickEventFilter* clickFilter_right = new ClickEventFilter(this);

    ui->label_left->installEventFilter(clickFilter_left);
    ui->label_right->installEventFilter(clickFilter_right);

    m_leftisBig = false;
    m_rightisBig = false;

    connect(clickFilter_left, &ClickEventFilter::clicked, this, &Page_video::handleClicked_left);
    connect(clickFilter_right, &ClickEventFilter::clicked, this, &Page_video::handleClicked_right);

    m_bigimage = new BigImage();
    connect(m_bigimage, &BigImage::bigImgClicked, this,  [&](){ //关闭大图片窗口
        m_bigimage->close();
        m_leftisBig = false;
        m_rightisBig = false;
        this->show();
    });


//    m_getVideoThread = new GetVideoThread();
//    connect(m_getVideoThread, &GetVideoThread::imgReturned, this, &Page_video::showImg_left);



}

Page_video::~Page_video()
{
    delete ui;
}


void Page_video::on_pBtn_back_clicked()
{
    this->hide();
    emit backToMainPage();
}

void Page_video::on_pushButton_clicked() //推流
{
    ui->pushButton->setEnabled(false);
    QProcess *process = new QProcess(this);
    QString command = "mjpg_streamer";
    QStringList arguments;
    arguments << "-i" << "input_uvc.so -d /dev/video1 -r 320x240 -f 30 -yuv"
              << "-o" << "output_http.so -w /opt/www";
    process->start(command, arguments);
    if (!process->waitForStarted()) {
        qDebug() << "Failed to start mjpg_streamer";
    } else {
        qDebug() << "mjpg_streamer started";
    }
}

void Page_video::on_pushButton_2_clicked() //拉流
{
    if (ui->pushButton_2->isChecked()) {
        qDebug() << "拉流选中";
//        m_getVideoThread->start();

        QString url = "http://192.168.1.100:8080/?action=stream";
        cv::VideoCapture *cap = new cv::VideoCapture(url.toStdString());

        if (!cap->isOpened()) {
            qDebug() << "Failed to open stream.";
            ui->pushButton_2->setChecked(false);
            return;
        }

        QTimer *timer = new QTimer(this);
        connect(timer, &QTimer::timeout, [=]() {
            cv::Mat frame;
            (*cap) >> frame;
            if (frame.empty()) {
                timer->stop();
                return;
            }

            QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
            showImage(image.rgbSwapped());
        });

        timer->start(33); // 每33ms获取一帧图像，约30帧每秒


    }
}

void Page_video::showImg(QImage *image) //右窗口添加
{
    QSize labelSize = ui->label_right->size(); // 获取 QLabel 的大小
    QImage scaledImage = image->scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    if (m_rightisBig){
        QSize size;
        size.setWidth(728);
        size.setHeight(560);
        scaledImage = image->scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);

        m_bigimage->showImg(&scaledImage);
    } else {
        ui->label_right->setPixmap(QPixmap::fromImage(scaledImage));
    }


}

void Page_video::showImage(const QImage &image) //左窗口添加 实体image
{
    /* 显示图像 */
    QSize labelSize = ui->label_left->size(); // 获取 QLabel 的大小
    QImage scaledImage = image.scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    if (m_leftisBig){
        QSize size;
        size.setWidth(728);
        size.setHeight(560);
        scaledImage = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);

        m_bigimage->showImg(&scaledImage);
    } else {
        ui->label_left->setPixmap(QPixmap::fromImage(scaledImage));
    }


    m_saveImage = image;

}

void Page_video::showImg_left(QImage *image)  //左窗口添加，指针image
{
//    qDebug() << "showImg_left()能获得拉流的图片";

    QSize labelSize = ui->label_left->size(); // 获取 QLabel 的大小
    QImage scaledImage = image->scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    if (m_leftisBig){
        QSize size;
        size.setWidth(728);
        size.setHeight(560);
        scaledImage = image->scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);

        m_bigimage->showImg(&scaledImage);
    } else {
        ui->label_left->setPixmap(QPixmap::fromImage(scaledImage));
    }

}

void Page_video::on_pushButton_3_clicked() //检测火灾
{
    QByteArray message = "FIRE";
//    m_udpSocket->writeDatagram(message, QHostAddress("127.0.0.1"),9000);
    m_udpThread->writeTo(message);
    qDebug() << "信息已发送";

}

void Page_video::on_pushButton_4_clicked() //检测人群
{
    QByteArray message = "PEOPLE";
//    m_udpSocket->writeDatagram(message, QHostAddress("127.0.0.1"),9000);
    m_udpThread->writeTo(message);
    qDebug() << "信息已发送";

}

void Page_video::handleClicked_left()
{
    qDebug() << "点击QLabel";
    m_leftisBig = true;
    m_bigimage->show();
    this->hide();
}

void Page_video::handleClicked_right()
{
    qDebug() << "点击QLabel";
    m_rightisBig = true;
    m_bigimage->show();
    this->hide();
}
